package com.sghd.candy.module.fight.manager;

import com.sghd.common.ramcache.IEntity;
import com.sghd.common.ramcache.PerPersist;
import com.sghd.common.ramcache.anno.*;
import com.sghd.common.ramcache.enhance.Enhance;
import com.sghd.common.utils.json.JsonUtils;
import com.sun.javafx.binding.StringFormatter;
import org.hibernate.annotations.Type;

import javax.persistence.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author Allen
 * @date 2018/12/18
 */
@Entity
@Table(catalog = "bsdld")
@Cached(size = CachedSizes.DEFAULT, persister = @Persister(Persisters.PRE_HOUR))
@InitialConfig(type = InitialType.ALL)
public class LevelConfig implements IEntity<LevelId>, PerPersist {

    @EmbeddedId
    private LevelId levelId;
    private Long money;
    private Integer elementDrop;
    @Transient
    private Map<Integer, Float> boxKeyDrop;
    @Column(length = 30)
    private String boxKeyDropJson;
    @Type(type = "com.sghd.common.ramcache.orm.type.Map")
    private Map<String, Integer> gradeOne;
    @Type(type = "com.sghd.common.ramcache.orm.type.Map")
    private Map<String, Integer> gradeTwo;
    @Type(type = "com.sghd.common.ramcache.orm.type.Map")
    private Map<String, Integer> gradeThree;
    @Type(type = "com.sghd.common.ramcache.orm.type.Set")
    private Set<Integer> playerSkills;
    private Integer isSpecial;


    public LevelId getLevelId() {
        return levelId;
    }

    void setLevelId(LevelId levelId) {
        this.levelId = levelId;
    }

    public Long getMoney() {
        return money;
    }

    void setMoney(Long money) {
        this.money = money;
    }

    public Integer getElementDrop() {
        return elementDrop;
    }

    public void setElementDrop(Integer elementDrop) {
        this.elementDrop = elementDrop;
    }

    public Map<Integer, Float> getBoxKeyDrop() {
        return new HashMap<>(this.boxKeyDrop);
    }

    void setBoxKeyDrop(Map<Integer, Float> boxKeyDrop) {
        this.boxKeyDrop = boxKeyDrop;
    }

    public String getBoxKeyDropJson() {
        return boxKeyDropJson;
    }

    void setBoxKeyDropJson(String boxKeyDropJson) {
        this.boxKeyDropJson = boxKeyDropJson;
    }

    public Map<String, Integer> getGradeOne() {
        return new HashMap<>(this.gradeOne);
    }

    void setGradeOne(Map<String, Integer> gradeOne) {
        this.gradeOne = gradeOne;
    }

    public Map<String, Integer> getGradeTwo() {
        return new HashMap<>(this.gradeTwo);
    }

    void setGradeTwo(Map<String, Integer> gradeTwo) {
        this.gradeTwo = gradeTwo;
    }

    public Map<String, Integer> getGradeThree() {
        return new HashMap<>(this.gradeThree);
    }

    void setGradeThree(Map<String, Integer> gradeThree) {
        this.gradeThree = gradeThree;
    }

    public Set<Integer> getPlayerSkills() {
        return playerSkills;
    }

    void setPlayerSkills(Set<Integer> playerSkills) {
        this.playerSkills = playerSkills;
    }

    public String getLevelStr() {
        return String.format("%02d", this.getLevelId().getBigLevelId()) + "-" + String.format("%02d", this.getLevelId().getLevelId());
    }

    public Integer getIsSpecial() {
        return isSpecial;
    }

    public void setIsSpecial(Integer isSpecial) {
        this.isSpecial = isSpecial;
    }

    @Enhance
    void configMoney(long money) {
        this.money = money;
    }

    @Enhance
    void configElement(int dropCount) {
        setElementDrop(dropCount);
    }

    @Enhance
    void configBoxKey(int boxKeyId, float dropRate) {
        if (this.boxKeyDrop == null) {
            this.boxKeyDrop = new HashMap<>();
        }
        this.boxKeyDrop.put(boxKeyId, dropRate);
    }

    @Enhance
    void configGradeOne(String key, Integer value) {
        if (this.gradeOne == null) {
            this.gradeOne = new HashMap<>();
        }
        this.gradeOne.put(key, value);
    }

    @Enhance
    void configGradeTwo(String key, Integer value) {
        if (this.gradeTwo == null) {
            this.gradeTwo = new HashMap<>();
        }
        this.gradeTwo.put(key, value);
    }

    @Enhance
    void configGradeThree(String key, Integer value) {
        if (this.gradeThree == null) {
            this.gradeThree = new HashMap<>();
        }
        this.gradeThree.put(key, value);
    }

    @Override
    public LevelId getId() {
        return this.levelId;
    }

    @Override
    public void afterLoad() {
        this.boxKeyDrop = JsonUtils.string2Map(this.boxKeyDropJson, Integer.class, Float.class);
    }

    @Override
    public void beforePersist() {
        this.boxKeyDropJson = JsonUtils.object2String(this.boxKeyDrop);
    }
}
